from models import db, BaseModel
from werkzeug.security import generate_password_hash, check_password_hash


class User(BaseModel):
    __tablename__ = 'user'
    username = db.Column(db.String(64), unique=True, comment='用户名称')
    password_hash = db.Column(db.String(256), comment='用户密码')
    token = db.Column(db.String(256), unique=True, nullable=True, comment='Token')

    def set_password(self, password):
        self.password_hash = generate_password_hash(password)

    def validate_password(self, password):
        return check_password_hash(self.password_hash, password)

    def __init__(self, username):
        self.username = username

    def __repr__(self):
        return f'<User {self.username}>'
